package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ApActiveMapper;
import com.zhentao.model.common.app.dto.ApUserActiveFindAllDto;
import com.zhentao.model.common.app.vo.UserActiveAndActiveVO;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.app.pojo.UserActive;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.common.manger.pojo.Activity;
import com.zhentao.service.ApUserActiveService;
import com.zhentao.mapper.ApUserActiveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 薄超轩
* @description 针对表【user_active】的数据库操作Service实现
* @createDate 2024-10-10 18:07:36
*/
@Service
public class ApUserActiveServiceImpl extends ServiceImpl<ApUserActiveMapper, UserActive>
    implements ApUserActiveService {
@Autowired
private ApUserActiveMapper apUserActiveMapper;

@Autowired
private ApActiveMapper apActiveMapper;


    /**
     * app端：查看自己参加的活动
     * @param dto
     * @return
     */
    @Override
    public ResponseResult ApUserActiveFIndAll(ApUserActiveFindAllDto dto) {
        List<UserActiveAndActiveVO>list=apUserActiveMapper.ApUserActiveFIndAll(dto);
        return ResponseResult.okResult(list);
    }


    /**
     * app端：添加活动
     * @param userActive
     * @return
     */
    @Override
    public ResponseResult apUserActiveSave(UserActive userActive) {
        if (userActive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断参加人数是否上限
        Activity activity = apActiveMapper.selectById(userActive.getActiveId());
        if (activity.getNumMax().equals(activity.getNumNow())){
            return ResponseResult.errorResult(808, "参加人数已满");
        }


        //判断用户是否已经参加活动
        QueryWrapper<UserActive>wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(UserActive::getActiveId, userActive.getActiveId());
        UserActive userActive1 = apUserActiveMapper.selectOne(wrapper);

        if (userActive1==null){
            apUserActiveMapper.insert(userActive);
            //如果用户添加成功，活动响应的参加人数+1
            activity.setNumNow(activity.getNumNow()+1);
            apActiveMapper.updateById(activity);
            return ResponseResult.okResult("添加成功");
        }else {
            return ResponseResult.errorResult(808, "您已经参加过了");
        }
    }
}




